using UnityEngine;
using QFramework;

namespace Projectsurviva
{
	public partial class Ball : ViewController
	{
		void Start()
		{
			// Code Here
			SelfRigidbody2D.velocity =
				new Vector2(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f) * Random.Range(Global.BasketBallSpeed.Value - 2, Global.BasketBallSpeed.Value + 2)); ;
			Hurtbox.OnTriggerEnter2DEvent(collider =>
			{
				var hurEnemybox = collider.GetComponent<HurtBoxenemy>();
                if (hurEnemybox)
                {
                    if(hurEnemybox.Owener.CompareTag("Enemy"))
					{
						var enemy = hurEnemybox.Owener.GetComponent<IEnemy>();
						DamageSystem.CalculateDamage(Global.BasketBallDamage.Value, enemy);
						//enemy.Hurt(Global.BasketBallDamage.Value);
						if(Random.Range(0,1f)<0.5f && collider&& collider.attachedRigidbody && Play.Default )
						{
							collider.attachedRigidbody.velocity = (collider.NormalizedDirection2DFrom(this) * 5 + collider.NormalizedDirection2DFrom(Play.Default) * 10);

							

                        }
					}
                }
            });

		}
        private void OnCollisionEnter2D(Collision2D other)
        {
			var normal = other.GetContact(0).normal;
			if (normal.x > normal.y)
			{
				SelfRigidbody2D.velocity = new Vector2(SelfRigidbody2D.velocity.x, Mathf.Sign(SelfRigidbody2D.velocity.y) * UnityEngine.Random.Range(0.5f, 1.5f)
					* UnityEngine.Random.Range(Global.BasketBallSpeed.Value - 2, Global.BasketBallSpeed.Value + 2));
				SelfRigidbody2D.angularVelocity = UnityEngine.Random.Range(-360, 360);

			}
			else
			{
				var rb = GetComponent<Rigidbody2D>();
				rb.velocity =
					new Vector2(
						Mathf.Sign(rb.velocity.x) * Random.Range(0.5f, 1.5f) * Random.Range(Global.BasketBallSpeed.Value - 2, Global.BasketBallSpeed.Value + 2),
						rb.velocity.y);

				rb.angularVelocity = Random.Range(-360f, 360f);


			}
			AudioKit.PlaySound("078_ball");
        }
    }
}
